package org.business.accounting.components;

import javassist.bytecode.stackmap.Tracer;

import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.business.accounting.beans.CartBean;
import org.business.accounting.interfaces.BasicOrder;
import org.business.accounting.interfaces.BasicProduct;
import org.business.accounting.interfaces.Cart;
import org.business.accounting.pages.ShowCart;

@SuppressWarnings("unused")
public class Purchase {
	
	
	@Parameter(required = true)
	@Property
	private Cart cart;
	
	@Parameter(required = true)
	@Property
	private BasicOrder basicOrder;


	@Component(parameters = { "object = basicOrder.item" })
	private BasicDisplay display;

	@Component
	private Form addToCartForm;

	@Property
	private int qty;
	
	@Inject
	private Messages messages;

	@Component(parameters = { "value = qty" })
	private TextField qtyField;
	
	public Object onSubmitFromAddToCartForm() {
		if( qty < 1 ) {
			addToCartForm.recordError(messages.get("quantity-less-than-one"));
			return null;
		}
		
		if ( qty > basicOrder.getItem().getAmount() ) {
			addToCartForm.recordError(messages.get("quantity-biger-than-pamount"));
			return null;
		}

		basicOrder.setQty(qty);
		basicOrder.calculatePrice();
		basicOrder.setCart(cart);

		basicOrder.getItem().add(basicOrder);

		cart.add(basicOrder);

		return ShowCart.class;
	}

}
